##! /bin/bash
#! bash
#
#  $Id: simple.sh 3 2010-05-27 16:25:49Z alistair.mills@btinternet.com $
#

### Main script starts here ###
# Store file name here

# ./simple.sh 2005_02_cul.txt tblParagraph_1.sql and tblParagraph_1.csv # this creates tblParagraph_1.sql and  tblParagraph_1.csv

export             INPUT=$1
export           OUTPUT2=$2
export           OUTPUT3=$3
export   tmp_file_family=/tmp/file_for_alistair_
export               sql=sql
export               csv=csv
# echo $INPUT $OUTPUT
export  sql_file=$OUTPUT2
export  csv_file=$OUTPUT3

[ -f $sql_file ] && rm $sql_file
[ -f $csv_file ] && rm $csv_file

# exit

     export number=0
     export file=1
     export k=0
     export tmp_file=${tmp_file_family}_${file}.txt
     export articleID=1
     export LANGUAGE=english
     while read content
     do
       k=`expr $k + 1`
       if [ "$content" != "" ]
       then
         export number=`expr $number + 1`
         echo $content >> $tmp_file
       else
         # echo file=$file tmp_file=$tmp_file number=$number wc=`wc -l $tmp_file`
         export file=`expr $file + 1`
         export tmp_file=${tmp_file_family}_$file.txt
       fi
     done < $INPUT

     k=0
     while [ $k -lt $file ]
     do
       export k=`expr $k + 1`
       export tmp_file=${tmp_file_family}_$k.txt
       export lines=`cat $tmp_file | wc -l`
       export n=0
       # echo reading file $tmp_file , $file
       export start_csv='"'
       export start_sql="insert into \`tblParagraph\` (\`articleID\` , \`number\`, \`language\`,  \`content\` ) values ( "
       export end_csv='"'
       export end_sql=');'

       while read content2
       do
         export n=`expr $n + 1`
         export content2="`echo $content2 | sed 's|"|\\\\"|g'`"
         if [ $lines -eq 1 ]
         then
           echo            ";${articleID};${k};\"$LANGUAGE\";${start_csv}${content2}${end_csv}"          >>  $csv_file
           echo "${start_sql}${articleID},${k},\"$LANGUAGE\",${start_csv}${content2}${end_csv}$end_sql"  >>  $sql_file
         fi

         if [ $lines -gt 1 -a $n -eq 1 ]
         then
           echo            ";${articleID};${k};\"$LANGUAGE\";${start_csv}${content2}"                    >>  $csv_file
           echo "${start_sql}${articleID},${k},\"$LANGUAGE\",${start_csv}${content2}"                    >>  $sql_file
         fi

         if [ $n -gt 1 -a $n -lt $lines ]
         then
           echo                                                         "${content2}"                    >>  $csv_file
           echo                                                         "${content2}"                    >>  $sql_file
         fi

         if [ $n -gt 1 -a $n -eq $lines ]
         then
           echo                                                         "${content2}$end_csv"            >>  $csv_file
           echo                                                         "${content2}${end_csv}$end_sql"  >>  $sql_file
         fi
       done < $tmp_file
     done

     #ls -l ${tmp_file_family}_*
     #rm    ${tmp_file_family}_*

#
# - the end -
#
